package com.marry.api;

import com.marry.entity.Message;
import com.marry.model.Result;
import com.marry.repository.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;

/**
 * 接口
 */
@RestController
@RequestMapping("/api/message")
public class MessageApi {

    @Autowired
    private MessageRepository messageRepository;

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody Message message) {
        message.setTime(new Timestamp(System.currentTimeMillis()));
        messageRepository.save(message);
        return Result.ok(list());
    }

    /**
     * 获取
     */
    @PostMapping("/list")
    public Result list() {
        List<Message> list = messageRepository.findAll();
        return Result.ok(list);
    }
}
